package org.csu.mybigpro.common;
//通用响应
//给出泛型


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import org.springframework.web.bind.annotation.PutMapping;

//注意，对每一个值都要提供get方法，因为需要get方法获取每一个值，拼接字符串，序列化为JSON，传过去
@Getter
//这就使得fastJson在序列化的时候，那些空值都不去管它
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommonResponse<T> {
    //比如用0表示成功，用1表示失败
    private final int status;
    private final String message;
    //相当于模板
    private T data;
    //只告诉你成功或者失败
    private CommonResponse(int status,String message)
    {
        this.status=status;
        this.message=message;
    }



    private CommonResponse(int status, T data,String message)
    {
        this.status=status;
        this.data=data;
        this.message=message;
    }
    //而这个也会被默认的序列化（所有的public的方法都会被序列化获取值）
    //加上这个注解，让他被忽略掉
    @JsonIgnore
    public boolean isSuccess(){
        return this.status == ResponseCode.SUCCESS.getCode();
    }
    //由于构造方法中T为String时会引起歧义，所以，将它们私有化，通过另外的不同名字的接口函数来调用即可
    //如果为成功
    public static <T>CommonResponse<T> createForSuccess(){
        //用常数的话可维护性很差,因此用枚举类型
//        return new CommonResponse<>(0,"请求成功");
        return new CommonResponse<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getDesc());
    }

    //
    public static <T>CommonResponse<T>  createForSuccessMessage(String message)
    {
        return new CommonResponse<>(ResponseCode.SUCCESS.getCode(), message);
    }
    public static <T>CommonResponse<T> createForSuccess(T data)
    {
        return new CommonResponse<>(ResponseCode.SUCCESS.getCode(),data,ResponseCode.SUCCESS.getDesc());
    }

    public static <T>CommonResponse<T> createForSuccess(String message,T data)
    {
        return new CommonResponse<>(ResponseCode.SUCCESS.getCode(),data,message);
    }

    public static <T>CommonResponse<T> createForError(){
        return new CommonResponse<>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
    }

    public static <T>CommonResponse<T> createForError(String message){
        System.out.println("333");
        System.out.println(message);
        return new CommonResponse<>(ResponseCode.ERROR.getCode(), message);
    }

    public static <T>CommonResponse<T> createForError(int code,String message){
        return new CommonResponse<>(code, message);
    }
}